%==========================================================================
%=  This file is part of the Sound Restoration Project
%=  (c) Copyright Industrial Mathematics Institute
%=                University of South Carolina, Department of Mathematics
%=  ALL RIGHTS RESERVED
%=
%=  Author: Borislav Karaivanov
%==========================================================================

%==========================================================================
% List of the files on which this procedure depends:
%
% createMissingDirs.m 
% generateOutputDirectory.m 
% getBitDepthFromVideoObject.m
% getFileSequenceFromRepresentative.m 
% getImagesFromImages.m 
% getImagesFromMovie.m 
% loadPresetsToProject.m 
% mergeRangeIntoSeparatedRanges.m 
% patientlyKeepTryingToReadUntilErrorFree.m 
% readAndCheckVideoFrames.m 
% readImageFile.m 
% readImagesByIndexRange.m  
% readVideoFrames.m 
% readdpx.m 
% secureReadableImages.m 
% setUpFactoryPresets.m 
% setUpProjectDirectories.m 
% setUpProjectWithFfmpegReadableMovieOrImage.m 
% setUpProjectWithMatlabReadableMovie.m 
% setUpProjectWithMatlabReadableOrDpxImage.m 
% Image processing toolbox
% ffmpeg
%
%==========================================================================

%==========================================================================
% The function "analyzeVideoInput" takes a string that could specify a
% movie or an image file and tries to determine which the case is. It
% identifies the following case: the file does not exist; the file is a
% movie in a format that is readable by Matlab; the file is an image in a
% format that is readable by Matlab; the file is a .dpx image (readable by
% a third-party reader). In the last two cases the function also determines
% the maximal run of image files with consecutive indices that match and
% include the specified template name.
% INPUT: "inFileName" is a string specifying a movie or an image file.
% OUTPUT: "projectStruct" returns a structure whose fields store the
% information relevant to the situation.
%==========================================================================
function projectStruct = analyzeVideoInput(inFileName, projectStruct)
% projectStruct = analyzeVideoInput('C:\E\NEHScans\NEH009.avi')
% projectStruct = analyzeVideoInput('C:\E\TEST000002.pgm')
% projectStruct = analyzeVideoInput('C:\E\TEST000002.tga')
% projectStruct = analyzeVideoInput('C:\E\NEHScans\NEH009\DPX\NEH009_000002.dpx')
% projectStruct = analyzeVideoInput('C:\E\NEHScans\NEH129ProRes4444\NEH129_ProRes444.mov')

if (nargin < 2)
    % Set up a preset project structure.
    projectStruct = setUpFactoryPresets();
end
    
projectStruct.inFile.name = inFileName;

% Check if the specified file exists.
if (exist(inFileName, 'file') ~= 2)
    projectStruct.inFile.kind = 'not existing';
    return;
end

%projectStruct = setUpProjectWithFfmpegReadableMovieOrImage(projectStruct);
      
% Try to read the given file using the Matlab built-in functionality.
try
    % Try to read the given file as a Matlab-readable movie file.
    projectStruct = setUpProjectWithMatlabReadableMovie(projectStruct);
catch
    try
        % Try to read the given file as a Matlab-readable or .dpx image
        % file.
        projectStruct = setUpProjectWithMatlabReadableOrDpxImage(projectStruct);
    catch
        try
            % Try to read the given file as an ffmpeg-readable movie of
            % image file.
            projectStruct = setUpProjectWithFfmpegReadableMovieOrImage(projectStruct);
        catch
            projectStruct.inFile.kind = 'unknown';
            return;
        end
    end
end

return;
% end of the function "analyzeVideoInput"
